package com.dpj.edu.controller;


import com.dpj.commonutils.Result;
import com.dpj.edu.client.VodClient;
import com.dpj.edu.entity.Video;
import com.dpj.edu.service.VideoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-01-27
 */

@RestController
@RequestMapping("/edu/video")
public class VideoController {
    @Autowired
    VodClient vodClient;
    @Autowired
    private VideoService videoService;

    //添加小节
    @PostMapping("addVideo")
    public Result addVideo(@RequestBody Video video) {
        videoService.save(video);
        return Result.ok();
    }

    //删除小节
    // TODO 后面这个方法需要完善：删除小节时候，同时把里面视频删除
    @DeleteMapping("{id}")
    public Result deleteVideo(@PathVariable("id") String id) {
        Video video = videoService.getById(id);
        String videoSourceId = video.getVideoSourceId();
        if(!StringUtils.isEmpty(videoSourceId)){
            vodClient.removeAlyVideo(videoSourceId);
        }
        videoService.removeById(id);
        return Result.ok();
    }

    //修改小节 TODO
    @PostMapping("updateVideo")
    public Result updateVideo(@RequestBody Video video){
        videoService.updateById(video);
        return Result.ok();

    }
    @GetMapping("get/{id}")
    public Result getVideoById(@PathVariable String id){
        Video video = videoService.getById(id);
        return Result.ok().data("video",video);
    }
}

